package com.example.demo.mvc;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;


import com.example.demo.entity.User;
import com.example.demo.service.RoleService;
import com.example.demo.service.UserService;
import com.example.demo.util.Md5util;

@Controller
@RequestMapping("/getUsers" )
@SessionAttributes(value={"loname","password"})
public class UserAction {

		@Autowired
		private UserService userService;
	
		
		private RoleService roleService;
		
		
		
		private List<User> list;
		
		private String password;
		
		private int aw;
		
		private int pid;
		
		private int rid;
		
		private User user1;
		
		private int uid;
		
		private int sid;
		
		private ModelAndView modelAndView;
		
		private String loname;
		
		private String uuid;
		
		@RequestMapping("/insertUser")
		public String insertUser(@ModelAttribute("user")User user){
		    password=user.getPassword();
		    uuid=UUID.randomUUID().toString();
		    password=Md5util.Md5(password);
		    user.setUuid(uuid);
		    
		    user.setPassword(password);
		    aw=userService.insertUser(user);
		    return Asd(aw,"/login","/error");
		}
				
		@RequestMapping("/deleteUser/{uid}")
		public String deleteUser(@PathVariable("uid")int uid){
			aw=userService.deleteUser(uid);
			return Asd(aw,"/selectUser","/error");
		}
		
		@RequestMapping("/updateUser")
		public String updateUser(@ModelAttribute("user")User user){
			aw=userService.updateUser(user);
			return Asd(aw,"/updateUser","/error");
		}
		
		@RequestMapping("/selectUser")
		public ModelAndView selectUser(){
			list=new ArrayList<User>();
			list=userService.selectUser();
			modelAndView= new ModelAndView();
			modelAndView.setViewName("selectUser");
			modelAndView.addObject("list",list);
			return modelAndView;
		}
		
		@RequestMapping("/selectOneUser")
		public ModelAndView selectOneUser(@RequestParam("uid")int uid){
			user1= new User();
			user1=userService.selectOneUser(uid);
			modelAndView= new ModelAndView();
			modelAndView.setViewName("selectOneUser");
	        modelAndView.addObject("user1",user1);
	        return modelAndView; 
		}
		
		@RequestMapping("/login")
		public String login(@ModelAttribute("user")User user,Model model){
			uuid=userService.selectUUIDAndName(user.getLoname());
			if(uuid!=null){
				model.addAttribute("noRegister","抱歉,您还没有进行邮箱验证,无法登陆");
				return "/login";
			}else{
				password= Md5util.Md5(user.getPassword());
				user.setPassword(password);
				user1=userService.selectNameAndPassword(user);
				if(user1!=null){
					loname=user1.getLoname();
					password=user1.getPassword();
					return "/home";
				}
				model.addAttribute("noUser","抱歉,您输入的用户名或者密码错误,请核对登录信息");
				return "/login";
			}
		}
		
		@RequestMapping("/registerUser")
		public ModelAndView updateUUID(@RequestParam("uuid")String uuid){
			user1= new User();
			System.out.println(uuid);
			user1= userService.selectUUID(uuid);
			System.out.println(user1.toString());
			aw=userService.updateUUID(user1);
			modelAndView= new ModelAndView();
			if(aw==1){
				modelAndView.setViewName("selectOneUser");
				modelAndView.addObject("user",user1);
				
				return modelAndView;
				
			}
			modelAndView.setViewName("error");
			modelAndView.addObject("errormes","抱歉,激活邮箱失败");
			return modelAndView;
			
		}
		
		
		@RequestMapping(value="/updatePassword",method=RequestMethod.POST)
		public String updatePassword(@RequestParam("password")String password,Model model){
			aw=userService.updatePassword(password);
			return Asd(aw,"/home","/error");
			
		}
		
		@RequestMapping("/updateName/")
		public String updateName(@ModelAttribute("user")User user){
			aw=userService.updateName(user);
			return Asd(aw,"/home","/error");
		}
		
		@RequestMapping("/selectUid")
		public ModelAndView selectUid(@ModelAttribute("user")User user){
			user1= new User();
			aw=userService.selectUid(user);
			modelAndView= new ModelAndView();
			modelAndView.setViewName("selectOneUser");
			modelAndView.addObject("uid",aw);
			return modelAndView;
			
		}
		
		
		/*@RequestMapping("/selectMyUser")
		public ModelAndView selectMyUser(@RequestParam("uid")int uid,@RequestParam("sid")int sid,HttpServletRequest request){
			HttpSession session= request.getSession();
			ModelAndView modelAndView= new ModelAndView();
			loname= (String)session.getAttribute("loname");
			pid=userService.selectPid(loname);
			rid= new Integer((String)request.getParameter("select"));
			roleAndPermission= new RoleAndPermission();
			roleAndPermission.setPid(pid);
			roleAndPermission.setRid(rid);
			RoleAndPermission roleAndPermission= new RoleAndPermission();
			roleAndPermission= roleAndPermissionService.selectOneRoleAndPermission(roleAndPermission);
			if(roleAndPermission!=null){
				map=studentService.selectSdPost(uid,sid);
				if(map!=null){
					modelAndView.setViewName("selectOnePost");
					modelAndView.addObject("singMap",map);
					return modelAndView;	
				}
				modelAndView.setViewName("error");
				modelAndView.addObject("errormes","抱歉,没有您要查询的序号");
				return modelAndView;
			}else{
				modelAndView.setViewName("error");
				modelAndView.addObject("errormes","抱歉,您的操作权限不足");
				return modelAndView;
			}
		}
		
		@RequestMapping("/selectCtUser")
		public ModelAndView selectCtPost(@RequestParam("ptid")int ptid,@RequestParam("ct")String ct,HttpServletRequest request){
			HttpSession session= request.getSession();
			ModelAndView modelAndView= new ModelAndView();
			loginname=(String)session.getAttribute("loginname");
			gid=userService.selectGid(loginname);
			rid= new Integer((String)request.getAttribute("select1"));
			gradeAndRole1= new GradeAndRole();
			gradeAndRole1.setGid(gid);
			gradeAndRole1.setRid(rid);
			GradeAndRole gradeAndRole = new GradeAndRole();
			gradeAndRole=gradeAndRoleService.selectOneGradeAndRole(gradeAndRole1);
			if(gradeAndRole!=null){
				map1= postService.selectSePost(ptid,ct);
				if(map1!=null){
					modelAndView.setViewName("selectOnePost");
					modelAndView.addAllObjects(map1);
					return modelAndView;
				}
				modelAndView.setViewName("error");
				modelAndView.addObject("errormes","抱歉没有您要查询的内容");
				return modelAndView;
			}else{
				modelAndView.setViewName("error");
				
				modelAndView.addObject("errormes","抱歉,您的操作权限不足");
				return modelAndView;
			}
		}*/
		
		
		public String Asd(int aw,String args1,String args2){
			if(aw==1){
				return args1;
			}else{
				return args2;
			}
			
		}
		
		/**
		 * 连接页面的方法
		 * 
		 */
		
		@RequestMapping("/")
		public String index(){
			
			return "/index";
		}
		
		@RequestMapping("/insertUser1")
		public String insertUser2(){
			
			return "/insertUser1";
		}
		
		@RequestMapping("/login1")
		public String login(){
			
			return "/login";
		}
		
		
	}


